; A nslookup GUI.
; Author: Roger Xu
; Created: 2011-02-14
; Last Modified: 2011-02-14


DNS_SERVERS := "默认设置=||OpenDNS=208.67.222.222|Google=8.8.8.8|台湾中华电信=168.95.1.1|上海电信主力=180.168.255.18|上海电信备用=116.228.111.118"

GetDnsServerNames(serverlist)
{
    serverNames := RegExReplace(serverlist, "=[^\|]*", "") ; "Google=8.8.8.8|" => "Google"
    return serverNames
}

GetDnsServerIP(serverlist, index)
{
    serverlist := RegExReplace(serverlist, "\|[\|]+", "|") ; strip extra delimiters

    ;StringSplit, OutputArray, InputVar [, Delimiters, OmitChars]
    StringSplit, servers, serverlist, |
    server := servers%index%
    serverIP := RegExReplace(server, "^[^=]+=", "") ; remove "server_name="
    return serverIP
}

CMD_PID=-1

nslookup(domain, server, ByRef CMD_PID)
{
    ;MsgBox, CMD_PID=%CMD_PID%

    ;IfWinNotExist [, WinTitle, WinText, ExcludeTitle, ExcludeText]
    IfWinNotExist, ahk_pid %CMD_PID%
    {
        ;MsgBox, run new cmd

        ;Run, Target [, WorkingDir, Max|Min|Hide|UseErrorLevel, OutputVarPID]
        Run, %comspec%, , , CMD_PID ; run a new cmd.exe process

        WinWait, ahk_pid %CMD_PID%, , 3
        if ErrorLevel
        {
            MsgBox, WinWait %comspec% timed out.
            return
        }
    }

    ; activate cmd window and type
    ;WinActivate [, WinTitle, WinText, ExcludeTitle, ExcludeText]
    WinActivate, ahk_pid %CMD_PID% ; activate the cmd window.
    ;WinWaitActive [, WinTitle, WinText, Seconds, ExcludeTitle, ExcludeText]
    WinWaitActive, ahk_pid %CMD_PID%, , 3 ; wait to activate the cmd window.
    if ErrorLevel
    {
        MsgBox, WinWaitActive %comspec% timed out.
        return
    }

    Send nslookup %domain% %server% {Enter} ; type nslookup command and enter

    return CMD_PID
}

ping(address)
{
    ;Run, http://www.wipmania.com/pingfrom/%address%/?c=beijing
    Run, http://www.wipmania.com/en/ping/%address%/?point=world
    return
}

DNS_SERVER_NAMES := GetDnsServerNames(DNS_SERVERS)

;Gui, Add, ControlType [, Options, Text]
Gui, Add, Text, , Please enter the domain you want to lookup:
Gui, Add, Edit, w300 vDomain, docs.google.com
Gui, Add, Text, , Please enter the DNS server (optional):
Gui, Add, Edit, w300 vDnsServerIP, 
Gui, Add, Text, , DNS server presets:
Gui, Add, DropDownList, vDnsServerChoice AltSubmit gDnsServerOnChange, %DNS_SERVER_NAMES%
Gui, Add, Button, gNsLookup, NsLookup(&Q)
Gui, Add, Button, gPing, Ping(&P)

;Win+N to show dialog
#n::
;Gui, Show [, Options, Title]
Gui, Show, , NsLookup
return

;Update DNS Server IP textbox
DnsServerOnChange:

;GuiControlGet, OutputVar [, Sub-command, ControlID, Param4]
GuiControlGet, SelectedDnsServerIndex, , DnsServerChoice,
dnsServerIP := GetDnsServerIP(DNS_SERVERS, SelectedDnsServerIndex)
GuiControl, , DnsServerIP, %dnsServerIP% ; update new DNS IP in textbox

;MsgBox, DNS server set to: %dnsServerIP%
return

; Query DNS
NsLookup:
Gui, Submit, NoHide ; Save the input from the user to each control's associated variable.

GuiControlGet, domain, , Domain
GuiControlGet, server, , DnsServerIP
nslookup(domain, server, CMD_PID)
return

; Ping
Ping:
Gui, Submit, NoHide ; Save the input from the user to each control's associated variable.

GuiControlGet, address, , Domain
ping(address)
return
